package com.kaibes.orm.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaibes.orm.core.core.ObjectStringConverter;

@Component
public class ObjectStringConverterImpl implements ObjectStringConverter {

    @Autowired
    private ObjectMapper objectMapper;
    
    @Override
    public String object2String(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public <T> T string2Object(String source, Class<T> clazz) {
        try {
            return objectMapper.readValue(source, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}
